package com.xiaotu.spider.weibo.pageprocessor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.TVWeiBoExponentialModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

/**
 * @类名 TVWeiBoExponentialPageProcessor
 * @日期 2017年12月19日
 * @作者 王艳龙
 * @功能  作品微博指数爬取
 */
@Component("TVWeiBoExponentialPageProcessor")
@Scope("prototype")
public class TVWeiBoExponentialPageProcessor extends APageProcessor
{
	//微博 指数首页
    protected static final String weiBosearchUrl = "http://data.weibo.com/index/hotword?wid=4f76ahDNxy8B&wname=";
    //微博指数url集合
	private final static Map<String, String> weiBoUrls= new HashMap<String, String>();
	private final static String savePath = PropertiesUtil.getProperty("exponential.save.path");
	
    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return new Request(weiBosearchUrl + KeywordEncode.encode(target.getString(this
                .getNameField())));
    }

    protected String getNameField()
    {
        return "tvName";
    }

    protected String getIdField()
    {
        return "tvid";
    }

    protected String getTargetType()
    {
        return Constants.TargetType.TV;
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.TV_WEIBO;
    }

    @Override
    public String getPageDataType(Page page)
    {
        if (page.getHtml() == null || page.getHtml().toString().contains("http://data.weibo.com/index/hotword"))
            return Constants.PageDataType.INVALID_DATA;
        return Constants.PageDataType.WEIBO;
    }

    @Override
    public void process(Page page)
    {
        Map<String, Object> tvMap = page.getRequest().getExtras();
        if (Constants.PageDataType.INVALID_DATA.equals(getPageDataType(page)))
            this.dataProcess(page, tvMap);
    }

    private void dataProcess(Page page, Map<String, Object> tvMap)
    {
    	try {		
    	//微博热词趋势
    	weiBoUrls.put("getchartdata", "http://data.weibo.com/index/ajax/getchartdata?__rnd=1468545569650&month=1");
    	//实时数据  1小时
    	weiBoUrls.put("realtime1hour", "http://data.weibo.com/index/ajax/realtime1hour?__rnd=1468569413701");
    	//实时数据  24小时
    	weiBoUrls.put("realtime24hour", "http://data.weibo.com/index/ajax/realtime24hour?__rnd=1468569413701"); 
    	//关键词地域细分
    	weiBoUrls.put("keywordzone", "http://data.weibo.com/index/ajax/keywordzone?type=default&__rnd=1468569751021");
    	//人群定位
    	weiBoUrls.put("crowdpositioning", "http://data.weibo.com/index/ajax/getdefaultattributealldata?__rnd=1468571658985");
    	
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	//微博指数数据
		HttpClient client = new DefaultHttpClient();
		//发送get请求
        HttpGet request = new HttpGet(weiBosearchUrl+KeywordEncode.encode(page.getRequest().getExtra(this.getNameField()).toString()));
		HttpResponse response = client.execute(request);
		String cookie ="";
		
		for(Header responseheader: response.getHeaders("Set-Cookie")){
				cookie+=responseheader.getValue();
		}
		Map<String, String> headers= new HashMap<String, String>();
		headers.put("Cookie",cookie);
		headers.put("Referer", weiBosearchUrl+KeywordEncode.encode(page.getRequest().getExtra(this.getNameField()).toString()));
		//微博 循环json请求
		for (String keyword : weiBoUrls.keySet()) {
			String weiBoResult = HttpUtils
	                .httpGetJSONP(weiBoUrls.get(keyword),headers);
				resultMap.put(keyword, weiBoResult);
			}
		page.putField(TVWeiBoExponentialModel.class.getName(), new TVWeiBoExponentialModel(
				page.getRequest().getExtra(this.getIdField()).toString(), 
				page.getRequest().getExtra(this.getNameField()).toString(), 
				"wb_reciqushi="+(String)resultMap.get("getchartdata"),
				"wb_shishi1xiaoshishuju="+(String)resultMap.get("realtime1hour"), 
				"wb_shishi24xiaoshishuju="+(String)resultMap.get("realtime24hour"), 
				"wb_guanjiancidiyu="+(String)resultMap.get("keywordzone"), 
				"wb_renqundingwei="+(String)resultMap.get("crowdpositioning")));	
		//生成环境更换成   /data/index/weibo
		page.putField("path",savePath+"weibo");
    	} catch (IOException e) {
	    	throw new RuntimeException(e);
		}
    }
}
